package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.stock.RepairTicketParam;
import com.yunji.aggregate.model.vo.stock.RepairTicketStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.storage.business.RepairTicketBusiness;
import com.yunji.storage.dto.RepairAuditDTO;
import com.yunji.storage.dto.RepairTicketDTO;
import com.yunji.storage.vo.RepairTicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: RepairTicketController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-27 19:51:39      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-坏返模块")
@RestController
@RequestMapping("storage/repair")
public class RepairTicketController extends BaseController {

    @Resource
    RepairTicketBusiness business;

    @Resource
    StockAggregateService aggregateService;

    /**
     * @param param
     * @Title export
     * @Desc 导出坏返单
     * @Date 2024-10-10 14:58:12.263
     */
    @ApiOperation("导出坏返单")
//    @PreAuthorize("@ss.hasPermi('inventory:badReturn:export')")
    @PostMapping("/export")
    public void export(RepairTicketParam param) {
        List<RepairTicketStatisticsVO> list = aggregateService.searchRepairList(param);
        ExcelUtil<RepairTicketStatisticsVO> util = new ExcelUtil<>(RepairTicketStatisticsVO.class);
        util.exportExcel(response, list, "坏返单数据");
    }

    /**
     * 坏返列表
     *
     * @param param
     * @return
     */
    @ApiOperation("/坏返列表")
    @PostMapping("/list")
    public Response<List<RepairTicketStatisticsVO>> search(@RequestBody RepairTicketParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<RepairTicketStatisticsVO> list = aggregateService.searchRepairList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 新增坏返单
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增坏饭单")
    @PostMapping
//    @PreAuthorize("@ss.hasPermi('inventory:badReturn:add')")
    public Response<Long> create(@RequestBody RepairTicketDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * 坏返单详情
     *
     * @param
     * @return
     */
    @ApiOperation("坏返单详情")
    @GetMapping("/{repairId}")
    public Response<RepairTicketVO> getInfo(@PathVariable Long repairId) {
        return ResponseHelper.success(business.select(repairId));
    }

    /**
     * 审核坏返单
     *
     * @param
     * @return
     */
    @ApiOperation("审核坏返单")
    @PostMapping("/handleAudit")
    @PreAuthorize("@ss.hasPermi('inventory:badReturn:audit')")
    public Response<String> handleAudit(@RequestBody RepairAuditDTO dto) {
        business.audit(dto);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 取消报废单
     *
     * @param repairId
     * @return
     */
    @ApiOperation("取消坏返单")
    @PutMapping("/cancel")
    public Response<String> cancel(@RequestParam Long repairId) {
        business.cancel(repairId);
        return ResponseHelper.success("取消成功");
    }

    /**
     * 重新提交
     *
     * @param dto
     * @return
     */
    @ApiOperation("重新提交")
    @PostMapping("/commit")
    public Response<String> commit(@RequestBody RepairTicketDTO dto) {
        business.commit(dto);
        return ResponseHelper.success("提交成功");
    }


    /**
     * 保存草稿
     *
     * @param dto
     * @return
     */
    @ApiOperation("保存草稿")
    @PostMapping("/saveDraft")
    public Response<String> saveDraft(@RequestBody RepairTicketDTO dto) {
        business.saveDraft(dto);
        return ResponseHelper.success("新增成功");
    }

    /**
     * 获取草稿
     *
     * @param belongId
     * @return
     */
    @ApiOperation("获取草稿")
    @GetMapping("/getDraft")
    public Response<RepairTicketVO> getDraft(@RequestParam Long belongId) {
        return ResponseHelper.success(business.getDraft(belongId));
    }

    /**
     * 批量删除坏返单
     *
     * @return
     */
    @ApiOperation("删除报废单")
    @DeleteMapping("/{repairIds}")
//    @PreAuthorize("@ss.hasPermi('inventory:badReturn:remove')")
    public Response<String> delete(@PathVariable Long... repairIds) {
        business.delete(repairIds);
        return ResponseHelper.success("删除成功");
    }

}